package com.itheima.util;

import java.util.*;

/*
双色球工具类

多线程
如果多个线程对同一个数据同时操作   (安全隐患的 )
ArrayList
LinkedList
HashSet
TreeSet
HashMap
TreeMap
都是线程不安全的集合

 */
public class BallUtil {
    private  Random r = new Random();
    private  Vector<Integer> redBalls=new Vector<>(List.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16));
    private  Vector<Integer> blueBalls=new Vector<>(List.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33
    ));
    private  Integer getRed() {
        return redBalls.get(r.nextInt(redBalls.size()));
    }

    private  Integer getBlue() {
        return blueBalls.get(r.nextInt(blueBalls.size()));
    }

    private  Vector<Integer> getAllBlues() {
        Vector<Integer> set = new Vector<>();
        //生成6个篮球
        for (int i = 0; i < 6; ) {
            Integer blue = getBlue();
            /*
            下面写的代码将 人为的干预生成的过程
             */
            if (i >= 3) {
                boolean exist_1 = isExist(blue - 3, set);
                boolean exist_2 = isExist(blue - 2, set);
                boolean exist_3 = isExist(blue - 1, set);
                boolean exist_4 = isExist(blue + 1, set);
                boolean exist_5 = isExist(blue + 2, set);
                boolean exist_6 = isExist(blue + 3, set);
                if(exist_1&&exist_2&&exist_3){
                    continue;
                }
                if(exist_2&&exist_3&&exist_4){
                    continue;
                }
                if(exist_3&&exist_4&&exist_5){
                    continue;
                }
                if(exist_4&&exist_5&&exist_6){
                    continue;
                }
            }
            blueBalls.remove(blue);
            set.add(blue);
            i++;
        }
        return set;
    }


    //判断一个数在集合中是否存在
    private  boolean isExist(Integer i, Vector<Integer> set) {
        for (Integer integer : set) {
            if (integer == i) {
                return true;
            }
        }
        return false;
    }

    public  Integer[] getAllBalls(){
        Integer[] balls = new Integer[7];
        Vector<Integer> allBlues = getAllBlues();
        Collections.sort(allBlues);
        balls=allBlues.toArray(balls);
        balls[6]=getRed();
        return balls;
    }


}
